document.addEventListener('DOMContentLoaded', function() {
    const loginForm = document.getElementById('login-form');
    const registerForm = document.getElementById('register-form');
    
    // 登录表单提交
    if (loginForm) {
        loginForm.addEventListener('submit', function(e) {
            e.preventDefault();
            validateLoginForm();
        });
    }
    
    // 注册表单提交
    if (registerForm) {
        registerForm.addEventListener('submit', function(e) {
            e.preventDefault();
            validateRegisterForm();
        });
    }
    
    // 登录验证逻辑
    function validateLoginForm() {
        const username = document.getElementById('username').value.trim();
        const password = document.getElementById('password').value.trim();
        let isValid = true;
        
        if (username.length < 4) {
            showError('username-error', '用户名至少4个字符');
            isValid = false;
        } else {
            hideError('username-error');
        }
        
        if (password.length < 6) {
            showError('password-error', '密码至少6个字符');
            isValid = false;
        } else {
            hideError('password-error');
        }
        
        if (isValid) {
            alert('登录成功（模拟）！');
            window.location.href = 'index.html';
        }
    }
    
    // 注册验证逻辑
    function validateRegisterForm() {
        const username = document.getElementById('reg-username').value.trim();
        const email = document.getElementById('reg-email').value.trim();
        const password = document.getElementById('reg-password').value.trim();
        const confirmPassword = document.getElementById('reg-confirm-password').value.trim();
        let isValid = true;
        
        if (username.length < 4) {
            showError('username-error', '用户名至少4个字符');
            isValid = false;
        } else {
            hideError('username-error');
        }
        
        if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
            showError('email-error', '请输入有效的邮箱地址');
            isValid = false;
        } else {
            hideError('email-error');
        }
        
        if (password.length < 6) {
            showError('password-error', '密码至少6个字符');
            isValid = false;
        } else {
            hideError('password-error');
        }
        
        if (password !== confirmPassword) {
            showError('confirm-error', '两次输入的密码不一致');
            isValid = false;
        } else {
            hideError('confirm-error');
        }
        
        if (isValid) {
            alert('注册成功（模拟）！请登录');
            window.location.href = 'login.html';
        }
    }
    
    // 显示错误信息
    function showError(elementId, message) {
        const element = document.getElementById(elementId);
        element.textContent = message;
        element.style.display = 'block';
    }
    
    // 隐藏错误信息
    function hideError(elementId) {
        const element = document.getElementById(elementId);
        element.style.display = 'none';
    }
});